

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>nrgpy.utils.utilities &mdash; nrgpy 0.10.19 documentation</title>
  

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> nrgpy
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">nrgpy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>nrgpy.utils.utilities</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nrgpy.utils.utilities</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pathlib</span>
<span class="kn">import</span> <span class="nn">pickle</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">sys</span>


<div class="viewcode-block" id="affirm_directory"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.affirm_directory">[docs]</a><span class="k">def</span> <span class="nf">affirm_directory</span><span class="p">(</span><span class="n">directory</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;create directory if not exists</span>

<span class="sd">    print status to terminal</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">directory</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">directory</span><span class="p">)</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">parents</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">FileExistsError</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">pass</span></div>


<div class="viewcode-block" id="check_platform"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.check_platform">[docs]</a><span class="k">def</span> <span class="nf">check_platform</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;determine which operating system python is running on&quot;&quot;&quot;</span>
    <span class="kn">from</span> <span class="nn">sys</span> <span class="kn">import</span> <span class="n">platform</span>
    <span class="k">return</span><span class="p">(</span><span class="n">platform</span><span class="p">)</span></div>


<div class="viewcode-block" id="count_files"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.count_files">[docs]</a><span class="k">def</span> <span class="nf">count_files</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="n">filters</span><span class="p">,</span> <span class="n">extension</span><span class="p">,</span> <span class="n">show_files</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;counts the number of files in the first level of a directory</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    directory : str</span>
<span class="sd">        path of directory to be checked</span>
<span class="sd">    filters : str</span>
<span class="sd">        filter present in file to be checked</span>
<span class="sd">    extension : str</span>
<span class="sd">        secondary filter</span>
<span class="sd">    show_files : bool, optional</span>
<span class="sd">        if set to True, prints file name</span>
<span class="sd">    start_time :  int</span>
<span class="sd">        seconds; if set, use as reference; only count if file is newer than start_time</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="s2">&quot;start_time&quot;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
        <span class="n">start_time</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;start_time&quot;</span><span class="p">)</span>
    <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">file_list</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">for</span> <span class="n">dirpath</span><span class="p">,</span> <span class="n">subdirs</span><span class="p">,</span> <span class="n">files</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">directory</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="n">x</span><span class="p">)):</span>
                <span class="k">if</span> <span class="n">filters</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">extension</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">x</span><span class="o">.</span><span class="n">lower</span><span class="p">():</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getmtime</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dirpath</span><span class="p">,</span><span class="n">x</span><span class="p">))</span> <span class="o">&gt;</span> <span class="n">start_time</span><span class="p">:</span>
                                <span class="n">file_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
                                <span class="n">count</span> <span class="o">=</span> <span class="n">count</span> <span class="o">+</span> <span class="mi">1</span>

                        <span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
                            <span class="n">file_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
                            <span class="n">count</span> <span class="o">=</span> <span class="n">count</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">if</span> <span class="n">show_files</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">count</span><span class="p">,</span> <span class="n">file_list</span>
    <span class="k">return</span> <span class="n">count</span></div>


<div class="viewcode-block" id="date_check"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.date_check">[docs]</a><span class="k">def</span> <span class="nf">date_check</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">,</span> <span class="n">string</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;returns true if string date is between dates</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    start_date : str</span>
<span class="sd">        &quot;YYYY-mm-dd&quot;</span>
<span class="sd">    end_date : str</span>
<span class="sd">        &quot;YYYY-mm-dd&quot;</span>
<span class="sd">    string : str</span>
<span class="sd">        string including date to check</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">string</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;rld&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">&quot;_&quot;</span> <span class="ow">in</span> <span class="n">string</span><span class="p">:</span>
        <span class="n">date_format</span> <span class="o">=</span> <span class="s2">&quot;([0-9]</span><span class="si">{4}</span><span class="s2">\-[0-9]</span><span class="si">{2}</span><span class="s2">\-[0-9]</span><span class="si">{2}</span><span class="s2">)&quot;</span>
    <span class="k">elif</span> <span class="n">string</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;rwd&quot;</span><span class="p">):</span>
        <span class="n">date_format</span> <span class="o">=</span> <span class="s2">&quot;([0-9]</span><span class="si">{4}</span><span class="s2">[0-9]</span><span class="si">{2}</span><span class="s2">[0-9]</span><span class="si">{2}</span><span class="s2">)&quot;</span>
        <span class="n">string</span> <span class="o">=</span> <span class="n">string</span><span class="p">[</span><span class="mi">4</span><span class="p">:]</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="s2">&quot;%Y-%m-</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">end</span>   <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">end_date</span><span class="p">,</span> <span class="s2">&quot;%Y-%m-</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">TypeError</span> <span class="k">as</span> <span class="n">t</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">traceback</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">())</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">start_date</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">end_date</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">date_text</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">date_format</span><span class="p">,</span> <span class="n">string</span><span class="p">)</span>
        <span class="n">file_date</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">date_text</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;%Y-%m-</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">file_date</span> <span class="o">&gt;=</span> <span class="n">start</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">file_date</span> <span class="o">&lt;=</span> <span class="n">end</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="n">date_text</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">date_format</span><span class="p">,</span> <span class="n">string</span><span class="p">)</span>
        <span class="n">file_date</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">date_text</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">file_date</span> <span class="o">&gt;=</span> <span class="n">start</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">file_date</span> <span class="o">&lt;=</span> <span class="n">end</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">traceback</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">())</span>
        <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="draw_progress_bar"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.draw_progress_bar">[docs]</a><span class="k">def</span> <span class="nf">draw_progress_bar</span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">total</span><span class="p">,</span> <span class="n">start_time</span><span class="p">,</span> <span class="n">barLen</span><span class="o">=</span><span class="mi">45</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;simple text progress bar&quot;&quot;&quot;</span>
    <span class="n">percent</span> <span class="o">=</span> <span class="n">index</span> <span class="o">/</span> <span class="n">total</span>
    <span class="n">pad</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">total</span><span class="p">))</span>

    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\r</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
        <span class="s2">&quot;Time elapsed: </span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2"> | </span><span class="si">{}</span><span class="s2"> / </span><span class="si">{}</span><span class="s2"> [{:&lt;</span><span class="si">{}</span><span class="s2">}] </span><span class="si">{:.0f}</span><span class="s2">%</span><span class="se">\t</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">-</span> <span class="n">start_time</span><span class="p">)</span><span class="o">.</span><span class="n">seconds</span><span class="p">,</span> <span class="s2">&quot;s&quot;</span><span class="p">,</span>
            <span class="nb">str</span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="o">.</span><span class="n">rjust</span><span class="p">(</span><span class="n">pad</span><span class="p">),</span>
            <span class="n">total</span><span class="p">,</span>
            <span class="s2">&quot;=&quot;</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="n">barLen</span> <span class="o">*</span> <span class="n">percent</span><span class="p">),</span>
            <span class="n">barLen</span><span class="p">,</span>
            <span class="n">percent</span> <span class="o">*</span> <span class="mi">100</span>
        <span class="p">)</span>
    <span class="p">)</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span></div>


<div class="viewcode-block" id="linux_folder_path"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.linux_folder_path">[docs]</a><span class="k">def</span> <span class="nf">linux_folder_path</span><span class="p">(</span><span class="n">folder_path</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;assert folder_path ending with &#39;/&#39; &quot;&quot;&quot;</span>
    <span class="n">folder_path</span> <span class="o">=</span> <span class="n">folder_path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;\ &#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">folder_path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">folder_path</span> <span class="o">=</span> <span class="n">folder_path</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span>

    <span class="k">return</span> <span class="n">folder_path</span></div>


<div class="viewcode-block" id="windows_folder_path"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.windows_folder_path">[docs]</a><span class="k">def</span> <span class="nf">windows_folder_path</span><span class="p">(</span><span class="n">folder_path</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;convert &#39;/&#39; to &#39;\\&#39; in folder_path and assert ending in &#39;\\&#39;&quot;&quot;&quot;</span>
    <span class="n">folder_path</span> <span class="o">=</span> <span class="n">folder_path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">folder_path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">folder_path</span> <span class="o">=</span> <span class="n">folder_path</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span>

    <span class="k">return</span> <span class="n">folder_path</span></div>


<div class="viewcode-block" id="renamer"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.renamer">[docs]</a><span class="k">class</span> <span class="nc">renamer</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;for replacing duplicate column names after transpose&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>

        <span class="k">if</span> <span class="n">x</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">return</span> <span class="n">x</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">return</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">_</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">d</span><span class="p">[</span><span class="n">x</span><span class="p">])</span></div>


<div class="viewcode-block" id="save"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.save">[docs]</a><span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="n">reader</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;save reader as a Python pickle file, to be recalled later&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">filename</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">reader</span><span class="o">.</span><span class="n">site_number</span><span class="si">}</span><span class="s2">_reader.pkl&quot;</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">reader</span><span class="o">.</span><span class="n">serial_number</span><span class="si">}</span><span class="s2">_reader.pkl&quot;</span>

    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">pkl</span><span class="p">:</span>
        <span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">reader</span><span class="p">,</span> <span class="n">pkl</span><span class="p">,</span> <span class="n">protocol</span><span class="o">=</span><span class="n">pickle</span><span class="o">.</span><span class="n">HIGHEST_PROTOCOL</span><span class="p">)</span></div>


<div class="viewcode-block" id="load"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.load">[docs]</a><span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="n">site_number</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;recall a reader from a pickle file by site number or filename</span>

<span class="sd">    parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    site_number : str</span>
<span class="sd">        6-digit site number of stored reader OR spidar serial number</span>
<span class="sd">    filename : str</span>
<span class="sd">        full or relative path of pickle file</span>

<span class="sd">    returns</span>
<span class="sd">    -------</span>
<span class="sd">    object</span>
<span class="sd">        contents of pickle file</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">filename</span><span class="p">:</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">site_number</span><span class="si">}</span><span class="s2">_reader.pkl&quot;</span>

    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">pkl</span><span class="p">:</span>
        <span class="n">reader</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">pkl</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">reader</span></div>


<div class="viewcode-block" id="data_months"><a class="viewcode-back" href="../../../nrgpy.utils.html#nrgpy.utils.utilities.data_months">[docs]</a><span class="k">def</span> <span class="nf">data_months</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">end_date</span><span class="p">,</span> <span class="n">output</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;returns list of months for a date range in YYYY-mm-dd format</span>
<span class="sd">    </span>
<span class="sd">    parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    start_date : str or datetime</span>
<span class="sd">        YYYY-mm-dd formatted date or datetime object</span>
<span class="sd">    end_date : str or datetime</span>
<span class="sd">        must be same formatting as start_date or god help you</span>
<span class="sd">    output : str</span>
<span class="sd">        &quot;string&quot; or &quot;datetime&quot;; specify date types you want returned.</span>
<span class="sd">    </span>
<span class="sd">    returns</span>
<span class="sd">    -------</span>
<span class="sd">    list</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="ow">and</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;^\d</span><span class="si">{4}</span><span class="s2">\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])&quot;</span><span class="p">,</span> <span class="n">start_date</span><span class="p">):</span>
        <span class="n">start_year</span> <span class="o">=</span> <span class="n">start_date</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">start_month</span> <span class="o">=</span> <span class="n">start_date</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">start_day</span> <span class="o">=</span> <span class="n">start_date</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
        <span class="n">end_year</span> <span class="o">=</span> <span class="n">end_date</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">end_month</span> <span class="o">=</span> <span class="n">end_date</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">end_day</span> <span class="o">=</span> <span class="n">end_date</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
        
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">start_date</span><span class="p">,</span> <span class="n">datetime</span><span class="p">):</span>
        <span class="n">start_year</span> <span class="o">=</span> <span class="n">start_date</span><span class="o">.</span><span class="n">year</span>
        <span class="n">start_month</span> <span class="o">=</span> <span class="n">start_date</span><span class="o">.</span><span class="n">month</span>
        <span class="n">start_day</span> <span class="o">=</span> <span class="n">start_date</span><span class="o">.</span><span class="n">day</span>
        <span class="n">end_year</span> <span class="o">=</span> <span class="n">end_date</span><span class="o">.</span><span class="n">year</span>
        <span class="n">end_month</span> <span class="o">=</span> <span class="n">end_date</span><span class="o">.</span><span class="n">month</span>
        <span class="n">end_day</span> <span class="o">=</span> <span class="n">end_date</span><span class="o">.</span><span class="n">day</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;unsupported date type: </span><span class="si">{</span><span class="n">output</span><span class="si">}</span><span class="se">\n</span><span class="s2">use &#39;YYYY-mm-dd&#39; or datetime object&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="n">years</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">start_year</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">end_year</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span>
    
    <span class="n">months</span> <span class="o">=</span> <span class="p">[]</span>
    
    <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">years</span><span class="p">:</span>
        
        <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">(</span><span class="n">start_year</span><span class="p">):</span>
            <span class="n">_month0</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">start_month</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">_month0</span> <span class="o">=</span> <span class="mi">1</span>
            
        <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">(</span><span class="n">end_year</span><span class="p">):</span>
            <span class="n">_month12</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">end_month</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">_month12</span> <span class="o">=</span> <span class="mi">12</span>
            
        <span class="k">for</span> <span class="n">_m</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">_month0</span><span class="p">,</span> <span class="n">_month12</span><span class="o">+</span><span class="mi">1</span><span class="p">)):</span>
            
            <span class="k">if</span> <span class="n">output</span> <span class="o">==</span> <span class="s2">&quot;string&quot;</span><span class="p">:</span>
                <span class="n">months</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2">-</span><span class="si">{</span><span class="nb">str</span><span class="p">(</span><span class="n">_m</span><span class="p">)</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="si">}</span><span class="s2">-01&quot;</span><span class="p">)</span>

            <span class="k">elif</span> <span class="n">output</span> <span class="o">==</span> <span class="s2">&quot;datetime&quot;</span><span class="p">:</span>
                <span class="n">months</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">datetime</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">_m</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;unsupported output type: </span><span class="si">{</span><span class="n">output</span><span class="si">}</span><span class="se">\n</span><span class="s2">use &#39;string&#39; or &#39;datetime&#39;&quot;</span><span class="p">)</span>
                <span class="k">return</span> <span class="kc">False</span>
            
    <span class="k">return</span> <span class="n">months</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2020, NRG Systems, Technical Services.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>